package com.windy.i18n.sample.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Created by liyufeng on 2022/5/15
 */
//@RestController
@Controller
public class TestController {
    @Autowired
    private MessageSourceAccessor accessor;

    @RequestMapping("/test")
    @ResponseBody
    public Map<String, Object> test() {
        Map<String, Object> result = new HashMap<>();
        Locale locale = LocaleContextHolder.getLocale();
        System.out.println("test : " + locale.toString());
        result.put("login.fail.username", accessor.getMessage("login.fail.username"));
        result.put("login.fail", accessor.getMessage("login.fail"));
        result.put("login.success", accessor.getMessage("login.success"));
        return result;
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }
}
